import { defineStore } from 'pinia'
import { userAPI } from '@/api/user'
import { userAccountForm } from "@/types/user";

export const useUserStore = defineStore('user', {
  state: () => ({
    userToken:{},
    userBaseInfo:{},
    token: '',
    isLogin: false,
    error: null as string | null
  }),

  actions: {
    async getUserToken() {
      try {
        console.log("拿到用户token");
        this.token = "qewwqdfwefwe"
        return
        const res = await userAPI.getUserTokenApi()
        console.log(res);
        return
        this.userToken = res
        this.isLogin = true
      } catch (error) {
        this.error = (error as Error).message
      } finally {
        this.isLogin = false
      }
    },

    async getUserBaseInfo() {
      try {
        console.log(2);
        return
        const res = await userAPI.getUserBaseInfoApi()
        console.log(res);
      } catch (error) {
        this.error = (error as Error).message
      } finally {
        this.isLogin = false
      }
    },

    async handleUserRegister(form: userAccountForm) {
      console.log(form);
      console.log(4);
      
    }
    // 获取商品列表
    // async fetchProducts() {
    //   try {
    //     this.loading = true
    //     this.products = await productAPI.getProducts()
    //   } catch (error) {
    //     this.error = (error as Error).message
    //   } finally {
    //     this.loading = false
    //   }
    // },

    // // 添加商品
    // async addProduct(product: Omit<Product, 'id'>) {
    //   try {
    //     const newProduct = await productAPI.createProduct(product)
    //     this.products.push(newProduct)
    //   } catch (error) {
    //     this.error = (error as Error).message
    //     throw error // 抛出错误让组件处理
    //   }
    // }
  },

  getters: {
    // 计算商品总数
    // totalProducts: (state) => state.products.length
  }
})